<?php

function request_var($var, $default = false, $validate = false){

	if(isset($_REQUEST[$var]) and $_REQUEST[$var] !== ''){
		$value = $_REQUEST[$var];

		switch($validate){

			case 'int':
				$value = (int)(filter_var($value, FILTER_VALIDATE_INT)) ? $value : $default;
			break;

			case 'numeric':
				if(!is_numeric($value))
					$value = $default;
			break;

			case 'string':
				if(!is_string($value))
					$value = $default;
			break;

			default:
				if(is_array($validate) and !in_array($value, $validate))
					$value = $default;
			break;

		}

		return $value;
	}
	elseif($default)
		return $default;

}

function call_page($p = false){
	global $error, $page;

	if(!$p){
		$error->e404();
	}


	if(!file_exists("pages/{$p}_page.php")){
		$p = DEFAULT_PAGE;
	}
	require_once "pages/{$p}_page.php";
	$page = new $p;
	$page->display();
	return $page;
}

function pr($var){
	global $page;

	if($page and $page->layout)
		$page->addContent('<pre>' . print_r($var, true) . '</pre>');
	else
		echo '<pre>' . print_r($var, true) . '</pre>';

}

function _pr($var){
    echo '<pre>';
	exit(print_r($var, true));
}

function __pr($var){
	$type = gettype($var);
	if($type == 'string')
		$var = htmlentities($var, null, 'utf-8');
	exit('<pre>' . print_r($var, true) . '</pre>');
}

function __vd($var){
  echo '<pre>';
	var_dump($var);
	exit(0);
}

function getURLContent($url, $writeToFile = false, &$requestTime = 0){

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	if($writeToFile){
		$fh = fopen($writeToFile, 'w');
		curl_setopt($ch, CURLOPT_FILE, $fh);
	}

	$data = curl_exec($ch);
	$requestTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);

	curl_close($ch);

	if(isset($fh))
		@fclose($fh);

	return $data;
}

function getRev(){
	if (file_exists('.svn/entries')) {
		$svn = file('.svn/entries');

		if (is_numeric(trim($svn[3]))) {
			$version = $svn[3];
		} else { // pre 1.4 svn used xml for this file
			$version = explode('"', $svn[4]);
			$version = $version[1];
		}

		if (trim($svn[9])) {
			$date = $svn[9];
			$date = strtotime($date);
			$date = strftime('%x %R', $date);
		}
		$data['R_NUM'] = trim($version);
		$data['R_DATE'] = trim($date);

		unset ($svn);
		unset ($version);
		unset ($date);

	} else {
		return false;
	}
	return $data;
}

function valid_email($email)
{
	return filter_var($email, FILTER_VALIDATE_EMAIL);
}

function valid_password($pwd)
{
	return filter_var($pwd, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[\w]{4,}$/")));
}

function nicetime($date)
{
	$delta = (int)((time() - strtotime($date)));

	$timePhrases = array(
		'lessThanMinuteAgo' => 'kevesebb mint egy perce',
		'minuteAgo'=> 'egy perce',
		'minutesAgo'=> '%s perce',
		'hourAgo'=> 'egy órája',
		'hoursAgo'=> '%s órája',
		'dayAgo'=> 'tegnap',
		'daysAgo'=> '%s napja',
		'lessThanMinuteUntil'=> 'kevesebb mint egy perc múlva',
		'minuteUntil'=> 'körülbelül egy perc múlva',
		'minutesUntil'=> '%s perc múlva',
		'hourUntil'=> 'körülbelül egy óra múlva',
		'hoursUntil'=> 'körülbelül %s óra múlva',
		'dayUntil'=> '1 nap múlva',
		'daysUntil'=> '%s nap múlva'
	);

	$suffix = ($delta < 0) ? 'Until' : 'Ago';
	if ($delta < 0) $delta = $delta * (-1);

	$msg = ($delta > 60) ?
				($delta > 120) ?
				($delta > (45 * 60)) ?
				($delta > (90 * 60)) ?
				($delta > (24 * 60 * 60)) ?
				($delta > (48 * 60 * 60)) ?
				  'days'
				: 'day'
				: 'hours'
				: 'hour'
				: 'minutes'
				: 'minute'
				: 'lessThanMinute';

	switch($msg){
		case 'minutes': $delta = round($delta / 60); break;
		case 'hours':   $delta = round($delta / 3600); break;
		case 'days': 	$delta = round($delta / 86400); break;
	}

	return sprintf($timePhrases[$msg.$suffix], $delta);
}

function array_insert(array &$array, $item, $pos){
	if((count($array)-1) >= $pos){
		$front = array_slice($array, 0, $pos);
		$back = array_slice($array, $pos);
		if(!is_array($item))
			$item = array($item);
		$array = array_merge($front, $item, $back);
	}
	else {

		$array[] = $item;
	}
}

function array_remove(array &$array, $pos){
	$c = count($array) - 1;
  for($i = $pos; $i < $c; $i++) {
      $array[$i] = $array[$i+1];
  }
  unset($array[$c]);
}

function generate_pwd(){
	$rand = rand();
	return substr(md5($rand), 5, 6);
}

function redirect($url)
{
	header("Location: {$url}");
	exit(0);
}

function browserReqRedirect($url)
{
	global $browserReqs;

	$browser = style::getBrowser();

	if(preg_match('/google|yahoo|bing|facebook/i', $browser['userAgent']))
		return false;

	if(request_var('sc_unsupported_not_show', 0) == 0 && (!isset($browserReqs[$browser['browserName']]) || $browserReqs[$browser['browserName']] > $browser['browserVersion']))
		redirect($url);
}

function getSiteVariables()
{
	$uri = $_SERVER['REQUEST_URI'];
	$qs = preg_replace('/^\//', '', $uri);
	@list($_REQUEST['page'], $_REQUEST['value1'], $_REQUEST['value2'], $_REQUEST['value3']) = explode('/', $qs);
}

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

?>